﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Thauan_Juliano_World
{
    public class Grid
    {
        Random random;

        public int NUM_obstacles = 8;
        public int Row { get; set; }
        public int Column { get; set; }

        public Node[,] Nodes { get; set; }

        public Grid(int row, int column, List<WorldObject> worldObjects)
        {
            this.random = new Random();

            this.Row = row;
            this.Column = column;

            Nodes = new Node[this.Row, this.Column];

            for (var i = 0; i < this.Column; i++)
            {
                for (var j = 0; j < this.Row; j++)
                {
                    Nodes[j, i] = new Node(j, i, worldObjects);
                }
            }
        }

        public void Raffle(ref List<Node> list, ref List<Tree> trees)
        {
            list.Clear();
            for (var i = 0; i < this.Row; i++)
            {
                for (var j = 0; j < this.Column; j++)
                {
                    this.Nodes[i, j].plano.ChangeType(PlaneType.Normal);
                }
            }

            int obstacleCount = 0;

            while(obstacleCount < this.NUM_obstacles){
                
                int tmpI = random.Next(0, this.Nodes.GetLength(0));
                int tmpJ = random.Next(0, this.Nodes.GetLength(1));

                Node node = this.Nodes[tmpI, tmpJ];

                if(!list.Contains(node)) {

                    list.Add(node);
                    node.plano.ChangeType(PlaneType.Obstacle);
                    obstacleCount++;
                }
            }
        }
    }
}
